/**
 * Copyright (C) 2013 DaiKit.com - daikit4gxt module (admin@daikit.com)
 *
 *         Project home : http://code.daikit.com/daikit4gxt
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.daikit.daikit4gxt.client.action.standard;

import com.daikit.commons.shared.bean.BaseUser;
import com.daikit.daikit4gxt.client.DkMain;
import com.daikit.daikit4gxt.client.action.BaseAction;
import com.daikit.daikit4gxt.client.action.ChainActionCreator;
import com.daikit.daikit4gxt.client.controller.BaseMainController;
import com.daikit.daikit4gxt.client.log.BaseLogger;


/**
 * The after connect action. It returns {@link BaseMainController#getSpecificAfterConnectAction(BaseUser)} with a chain
 * action creator reloading current screen and doing other stuff.
 * 
 * @author tcaselli
 * @version $Revision$ Last modifier: $Author$ Last commit: $Date$
 */
public class BaseAfterConnectAction
{

	protected static final BaseLogger log = BaseLogger.getLog(BaseAfterConnectAction.class);

	/**
	 * Wrapper to {@link #get(BaseUser, BaseAction)} with a null actionToChain
	 * 
	 * @param previousLoggedUser
	 *           the previously logged user or null if no user was previously connected
	 * @return the base after connect action.
	 */
	public static final BaseAction<?> get(final BaseUser previousLoggedUser)
	{
		return get(previousLoggedUser, null);
	}

	/**
	 * If current screen == null it show the current screen and reload it.<br>
	 * Else If BaseMain.loadDefaultScreenAfterEachLogin() it show the default screen and reload it only if current logged
	 * user have changed.<br>
	 * Else it reloads the current screen only if current logged user have changed.<br>
	 * <br>
	 * If actionToChain is not null it is added at the end of the chain (only if current screen is not default screen and
	 * the default screen has to be shown after each connection). <br>
	 * 
	 * @param previousLoggedUser
	 *           the previously logged user or null if no user was previously connected
	 * @param actionToChain
	 *           the action to chain at the end of the after connect action.
	 * @return the base after connect action.
	 */
	public static final BaseAction<?> get(final BaseUser previousLoggedUser, final BaseAction<?> actionToChain)
	{
		final BaseAction<?> afterConnectAction = DkMain.controller().getSpecificAfterConnectAction(previousLoggedUser);

		afterConnectAction.setLastChainActionCreator(new ChainActionCreator()
		{
			@Override
			public BaseAction<?> getCreatedActions()
			{
				BaseAction<?> ret = null;
				if (DkMain.controller().getCurrentScreen() == null)
				{
					ret = BaseShowScreenAction.get(DkMain.controller().getDefaultScreen());
				}
				else
				{
					if (DkMain.config().loadDefaultScreenAfterEachLogin()
							&& !DkMain.controller().isCurrentScreen(DkMain.controller().getDefaultScreen()))
					{
						log.debug("BaseMain.loadDefaultScreenAfterEachLogin() && !BaseMain.controller().isCurrentScreen(BaseMain.controller().getDefaultScreen())");
						log.debug("BaseMain.controller().getDefaultScreen()=" + DkMain.controller().getDefaultScreen());
						log.debug("BaseMain.controller().getCurrentScreen()=" + DkMain.controller().getCurrentScreen().getClass());
						ret = BaseShowScreenAction.get(DkMain.controller().getDefaultScreen());
						ret.setLastChainActionCreator(new ChainActionCreator()
						{
							@Override
							public BaseAction<?> getCreatedActions()
							{
								return !DkMain.model().getUserLogged().equals(previousLoggedUser) ? BaseReloadCurrentScreenAction
										.get(true) : BaseInvalidateUiAction.get(true);
							}
						});
					}
					else if (!DkMain.model().getUserLogged().equals(previousLoggedUser))
					{
						ret = BaseReloadCurrentScreenAction.get(true);
					}
					else
					{
						ret = BaseInvalidateUiAction.get(false);
						ret.setLastChainAction(actionToChain);
					}
				}
				return ret;
			}
		});
		return afterConnectAction;
	}
}
